/*
 * Album.cpp
 *
 *  Created on: Jun 22, 2013
 *      Author: amatzlia
 */

#include "Album.h"
#include "mtm_ex4.h"

Album::Album(int barcode, int price, int year, int numPurchased, string title,
		string artist) :
		barcode(barcode), price(price), year(year), numPurchased(numPurchased), title(
				title), artist(artist) {
	if (barcode <= 0) {
		throw mtm::INVALID_BARCODE();
	}
	if (price <= 0) {
		throw mtm::INVALID_PRICE();
	}
	if (numPurchased < 0) {
		throw mtm::INVALID_NUM_PURCHASED();
	}
}

Album::Album(int barcode) :
		barcode(barcode), price(0), year(0), numPurchased(0), title(""), artist(
				"") {
	if (barcode <= 0) {
		throw mtm::INVALID_BARCODE();
	}
}

Album::Album() :
		barcode(0), price(0), year(0), numPurchased(0), title(""), artist("") {
}

string Album::getMinimalString() const {
	return mtm::getAlbumMinimalString(title, artist, year, price);
}

string Album::getFullString() const {
	return mtm::getAlbumFullString(barcode, price, year, numPurchased, title,
			artist);
}

int Album::purchase() {
	numPurchased++;
	return price;
}

bool Album::SortByBarcode::operator()(const Album& albumA,
		const Album& albumB) const {
	return albumA.barcode < albumB.barcode;
}

Album::BarcodeEquals::BarcodeEquals(const Album& album) :
		barcode(album.barcode) {
}

bool Album::BarcodeEquals::operator()(const Album& album) const {
	return album.barcode == barcode;
}

bool Album::SortByNumPurchasedThenBarcode::operator()(const Album& albumA,
		const Album& albumB) const {
	if (albumA.numPurchased != albumB.numPurchased) {
		return albumA.numPurchased > albumB.numPurchased;
	}
	return albumA.barcode < albumB.barcode;
}

bool operator==(const Album& albumA, const Album& albumB) {
	Album::SortByBarcode sorter;
	return !sorter(albumA,albumB) && !sorter(albumB,albumA);
}
